package org.workcraft.gui.propertyeditor;

import java.awt.Color;
import java.awt.Component;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.TableCellRenderer;

@SuppressWarnings("serial")
public class FileCellRenderer extends JLabel implements TableCellRenderer {
	    Border unselectedBorder = null;
	    Border selectedBorder = null;

	    public FileCellRenderer() {
	        setFocusable(false);
	    }

	    @Override
	    public Component getTableCellRendererComponent(JTable table, Object value,
	    		boolean isSelected, boolean hasFocus, int row, int column) {
	    	
	    	if (value != null) {
	    		File file = (File)value;
	    		if (file.exists()) {
	    			setForeground(Color.BLACK);
	    		} else {
	    			setForeground(Color.RED);
	    		} 
	    		setText(".../"+file.getName());
	    		if (isSelected) {
	    			if (selectedBorder == null) {
	    				selectedBorder = BorderFactory.createMatteBorder(
	    						2, 5, 2, 5,	table.getSelectionBackground());
	    			}
	    			setBorder(selectedBorder);
	    		} else {
	    			if (unselectedBorder == null) {
	    				unselectedBorder = BorderFactory.createMatteBorder(
	    						2, 5, 2, 5, table.getBackground());
	    			}
	    			setBorder(unselectedBorder);
	    		}
	    	}
	        return this;
	    }
}
